// System Unit
// Logging Support

(*************)
(* Interface *)
(*************)
{$IFDEF Include_Modules_Header}

 Type
  RTL_LogLevel = ( llNote, llInfo, llWarn, llError, llFatal );

 Type RTLProc_Log = Procedure (Const Level: RTL_LogLevel; Const Msg: PChar);
 Var Log: RTLProc_Log = NIL;

 //==================
 // RTL Extensions
 //==================
 Type
  TRTLEx_Logging = Record
   Log: RTLProc_Log;
  END;

 Procedure RTLExtend_Logging(Var Ex: TRTLEx_Logging);

{$ENDIF Include_Modules_Header}
(******************)
(* Implementation *)
(******************)
{$IFDEF Include_Modules_Implement}

 ResourceString
  _RTL_ResStr_LogNote  = '[Note]';
  _RTL_ResStr_LogInfo  = '[Info]';
  _RTL_ResStr_LogWarn  = '[Warning]';
  _RTL_ResStr_LogError = '[Error]';
  _RTL_ResStr_LogFatal = '[Fatal Error]';

 Const
  _INT_LogLevelMap: Array [RTL_LogLevel] of PResStringRec =
  (
   @_RTL_ResStr_LogNote,
   @_RTL_ResStr_LogInfo,
   @_RTL_ResStr_LogWarn,
   @_RTL_ResStr_LogError,
   @_RTL_ResStr_LogFatal
  );

 Procedure _RTL_Log(Const Level: RTL_LogLevel; Const Msg: PChar);
  Var
   LogMsg: Array [0..RTL_ResStr_MsgLen*4+RTL_ResStr_CapLen+1] of Char;
   MsgLen: Cardinal;
  BEGIN
   MsgLen:= LoadResStringFx(_INT_LogLevelMap[Level], LogMsg, RTL_ResStr_CapLen+1);
   LogMsg[MsgLen]:= ' '; Inc(MsgLen);
   RTL_PWideCharCopyNPtr(@LogMsg[MsgLen], Msg, RTL_ResStr_MsgLen+RTL_ResStr_CapLen-MsgLen+1)^:= #0;

   // Simply dump the output to debugger
   _WIN_OutputDebugString(LogMsg);
  END;

 Procedure _INT_LogResMsg(Const Level: RTL_LogLevel; Const ResMsg: PResStringRec);
  Var Msg: Array [0..RTL_ResStr_MsgLen] of Char;
  BEGIN
   LoadResStringFx(ResMsg, Msg, RTL_ResStr_MsgLen+1);
   Log(Level, Msg);
  END;

 //==================
 // RTL Extensions
 //==================

 Procedure RTLExtend_Logging(Var Ex: TRTLEx_Logging);
  BEGIN
   _INT_RTL_DoEx(@Log, @Ex.Log);
  END;

 Procedure _INT_Logging_Init;
  BEGIN
   Log:= _RTL_Log;
  END;

{$ENDIF Include_Modules_Implement}
(******************)
(* Initialization *)
(******************)
{$IFDEF Include_Modules_Initialize}
{$ENDIF Include_Modules_Initialize}
(****************)
(* Finalization *)
(****************)
{$IFDEF Include_Modules_Finalize}
{$ENDIF Include_Modules_Finalize}
